﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Silvermoon.Controls;
using Silvermoon.Utils;
using Silvermoon.Animations;
using Silvermoon.Shapes;
using Silvermoon.Images;
using Silvermoon.OpenGL;
using Silvermoon.Controls.Forms;
using Silvermoon.UI;
using Silvermoon.Windows;
using System.Diagnostics;
using Silvermoon.Windows.Styles;

namespace Testing
{
    public class DefaultControlsWindow : DemoWindowBase
    {
        const string openDlgCmd = "OpenDialog";
        const string closeCommand = "Close";

        private Toolbar myToolbar;
        private PopupMenu contextMenu;
        private ContextMenu modalContextMenu;
        private Label label;
        private TextBox tb;
        protected Slider toolbarOpacitySlider, readonlySlider;

        public DefaultControlsWindow()
            : base()
        {
            Initialize();
        }


        private void Initialize()
        {
            //this.Styles = new MetroStyles();

            SetTransition(400, TransitionMask.Fade | TransitionMask.Zoom, TransitionMask.Fade | TransitionMask.Explode);

            this.Color = Color.Empty;
            Background = new GradientShape { StartColor = PhoneColors.Orange, EndColor = PhoneColors.Yellow };

            modalContextMenu = new ContextMenu();
            modalContextMenu.Items.Add(new TextItem("MenuItem #1"));
            modalContextMenu.Items.Add(new TextItem("MenuItem #2"));
            modalContextMenu.Items.Add(new TextItem("MenuItem #3"));

            myToolbar = Toolbar as Toolbar;
            myToolbar.ItemsPerRow = Orientation == Orientation.Landscape ? 2 : 3;

            myToolbar.IsMenuButtonVisible = true;
            myToolbar.MenuItems.Add(new MenuItem { Text = "Details..." });
            myToolbar.MenuItems.Add(new MenuItem { Text = "Exit" });
            myToolbar.MenuItems.Add(new MenuItem { Text = "About..." });
            myToolbar.MenuItems.Add(new MenuItem { Text = "Item 1" });
            myToolbar.MenuItems.Add(new MenuItem { Text = "Item 2" });
            myToolbar.MenuItems.Add(new MenuItem { Text = "Item 3" });
            myToolbar.MenuItems.Add(new MenuItem { Text = "Item 4" });
            myToolbar.MenuItems.Add(new MenuItem { Text = "Item 5" });

            //myToolbar.MenuItems[0].Command = new InvokeCommand("Showing Details", (c, e) => ShowMessage(e.CommandName));
            for (int i = 1; i < myToolbar.MenuItems.Count; i++)
            {
                myToolbar.MenuItems[i].Tap += OnItemTap;
            }

            ScrollPanel scrollPanel = new ScrollPanel
            {
                Margin = Margin.Zero
            };
            Controls.Add(scrollPanel);


            label = new Label
            {
                Bounds = new Rectangle(16, 16, 400, 64)
            };

            GetMemory();

            scrollPanel.Controls.Add(label);

            tb = new TextBox
            {
                Text = "Tap and Hold me...",
                Bounds = new Rectangle(48, 96, 400, 64)
            };
            scrollPanel.Controls.Add(tb);


            Label slabel = new Label
            {
                Bounds = new Rectangle(48, 176, 400, 48),
                Text = "Toolbar opacity:"
            };
            scrollPanel.Controls.Add(slabel);

            toolbarOpacitySlider = new Slider
            {
                Minimum = 0,
                Maximum = 255,
                Value = 160,
                Bounds = new Rectangle(48, 224, 400, Slider.DefaultButtonSize),
            };

            readonlySlider = new Slider
            {
                Minimum = 0,
                Maximum = 255,
                Value = 160,
                Bounds = new Rectangle(48, 284, 400, Slider.DefaultButtonSize),
                FillColor = PhoneColors.Orange,
                Readonly = true
            };


            scrollPanel.Controls.Add(readonlySlider, toolbarOpacitySlider);

            AnimatedControl busy = new AnimatedControl
            {
                StartDelay = 3000,
                Velocity = 1.2f,
                Size = new Size(96, 96),
                Margin = new Margin(null, null, 4, 4),
                Color = PhoneColors.FromArgb(220, 200, 200, 220) //  Color.Gray
            };
            scrollPanel.Controls.Add(busy);

            toolbarOpacitySlider.ValueChanged += new EventHandler(OnSliderChanged);


            ToggleSwitch enableDisableToggle = new ToggleSwitch
            {
                IsChecked = true,
                Bounds = new Rectangle(16, 96 + 80 * 3, 160, 64),
                On = "Enable",
                Off = "Disable",
                ContextMenu = modalContextMenu,
            };
            scrollPanel.Controls.Add(enableDisableToggle);

            Label l2 = new Label { Text = "Toolbar: ", Bounds = new Rectangle(480 - 160 - 120, 96 + 80 * 3, 120, 64), LineAlignment = Alignment.Center };
            scrollPanel.Controls.Add(l2);

            ToggleSwitch toolbarToggle = new ToggleSwitch
            {
                IsChecked = true,
                Bounds = new Rectangle(480 - 160 - 8, 96 + 80 * 3, 160, 64),
                ButtonShape = new AccentShape { Color = Color.Green, Size = new Size(48, 2) },
                Background = new TextBoxShape { Color = PhoneColors.Green },
                On = "Show",
                Off = new StringShape { Text = "Hide", Color = Color.White, IsShadowed = true },
                TextColor = Color.Black,
                ContextMenu = modalContextMenu,
                Color = PhoneColors.Lime,
            };
            scrollPanel.Controls.Add(toolbarToggle);


            Button dlgButton = new Button
            {
                //Background = new HighQualityButtonShape { Color = Color.FromArgb(32, 32, 32) },
                Background = new MetalicButtonShape { Color = Color.FromArgb(32, 32, 32) },
                Bounds = new Rectangle(16, 96 + 80 * 4, 160, 64),
                Color = Color.FromArgb(32, 32, 32),
                Content = "Dialog",
            };

            scrollPanel.Controls.Add(dlgButton);

            Button imageButton = new Button
            {
                Bounds = new Rectangle(16 + 160 + 8, 96 + 80 * 4, 80, 64),
                Color = PhoneColors.DeepBlue,
                Content = SysImages.Back
            };
            imageButton.Tap += (s, e) =>
            {
                GC.Collect();
                GetMemory();
            };

            scrollPanel.Controls.Add(imageButton);

            Button accent1 = new Button
            {
                Bounds = new Rectangle(16 + 120 + 8 + 120 + 8, 96 + 80 * 4, 64, 64),
                Color = PhoneColors.Orange,
                Content = new StringShape { IsShadowed = true, Text = "1" },
                ContextMenu = modalContextMenu,
            };
            accent1.Tap += (s, e) =>
            {
                label.Text = GC.GetTotalMemory(true).ToString();
                toolbarOpacitySlider.Value = 220;
            };
            Button accent2 = new Button
            {
                Bounds = new Rectangle(16 + 120 + 8 + 120 + 8 + 64 + 8, 96 + 80 * 4, 64, 64),
                Color = PhoneColors.Blue,
                Content = new StringShape { IsShadowed = true, Text = "2" },
                ContextMenu = modalContextMenu,
            };
            accent2.Tap += (s, e) => toolbarOpacitySlider.Value = 255;
            Button accent3 = new Button
            {
                Bounds = new Rectangle(16 + 120 + 8 + 120 + 8 + (64 + 8) * 2, 96 + 80 * 4, 64, 64),
                Color = PhoneColors.Green,
                Content = ImagePool.Update, // SysImages.Check
                ContextMenu = modalContextMenu,
            };

            accent3.Tap += (s, e) => toolbarOpacitySlider.Value = 0;
            scrollPanel.Controls.Add(accent1);
            scrollPanel.Controls.Add(accent2);
            scrollPanel.Controls.Add(accent3);

            TextBox textBox2 = new TextBox
            {
                AutoHeight = true,
                Bounds = new Rectangle(48, 96 + 80 * 5, 400, 64),
                Text = "This is the second Textbox  which has a large text to scroll.",
            };
            scrollPanel.Controls.Add(textBox2);

            TextBox readonlyTextBox = new TextBox
            {
                ReadOnly = true,
                Text = "Readonly Text with clipboard",
                Bounds = new Rectangle(48, 96 + 80 * 6, 320, 64)
            };
            scrollPanel.Controls.Add(readonlyTextBox);

            CheckBox checkBox = new CheckBox
            {
                IsChecked = true,
                Bounds = new Rectangle(16, 96 + 80 * 7, 320, 64),
                Content = new StringShape { Text = "Check me...", Font = FontSprite.RegularShadowed },
                ContextMenu = modalContextMenu,

            };
            checkBox.CheckedChanged += (s, e) =>
            {
                readonlyTextBox.Text = checkBox.IsChecked ? "Checked" : "Unchecked";
            };
            scrollPanel.Controls.Add(checkBox);

            enableDisableToggle.CheckedChanged += (s, e) =>
            {
                bool enabled = enableDisableToggle.IsChecked;
                textBox2.Text = enabled ? "Enabled" : "Disabled";
                toolbarToggle.Enabled = enabled;
                label.Enabled = enabled;
                dlgButton.Enabled = enabled;
                imageButton.Enabled = enabled;
                accent1.Enabled = enabled;
                accent3.Enabled = enabled;
                tb.Enabled = enabled;
                checkBox.Enabled = enabled;
                toolbarOpacitySlider.Enabled = enabled;
            };

            toolbarToggle.CheckedChanged += (s, e) =>
            {
                textBox2.Text = toolbarToggle.IsChecked ? "Toolbar shown" : "Toolbar hidden";
                Toolbar = toolbarToggle.IsChecked ? myToolbar : null;
            };


            contextMenu = new PopupMenu { Width = 160, Height = 296 };
            contextMenu.Items.Add(new MenuItem { Text = "Red" });
            contextMenu.Items.Add(new MenuItem { Text = "Black" });
            contextMenu.Items.Add(new MenuItem { Text = "Blue" });
            contextMenu.Items.Add(new MenuItem { Text = "Close" });

            contextMenu.Items[0].Tap += (s, e) => (contextMenu.AnchorControl as Button).Color = Color.DarkRed;
            contextMenu.Items[1].Tap += (s, e) => (contextMenu.AnchorControl as Button).Color = Color.FromArgb(32, 32, 32);
            contextMenu.Items[2].Tap += (s, e) => (contextMenu.AnchorControl as Button).Color = PhoneColors.DeepBlue;

            dlgButton.ContextMenu = contextMenu;
            imageButton.ContextMenu = contextMenu;
        }

        protected virtual void OnSliderChanged(object sender, EventArgs e)
        {
            int v = toolbarOpacitySlider.Value;
            tb.Text = v.ToString();
            ToolbarOpacity = v;
            readonlySlider.Value = readonlySlider.Maximum - v;
        }

        private void GetMemory()
        {
            //            label.Text = GC.GetTotalMemory(true).ToString();
            var memory = NativeMemory.GetMemoryStatus();
            label.Text = "Available memory: " + memory.AvailableAsString;
        }

        protected override void Dispose(bool dispose)
        {
            DisposeObject(modalContextMenu);
            DisposeObject(contextMenu);
            base.Dispose(dispose);
        }

        void ShowMessage(string title)
        {
            MessageDialog.Show(title, "This is a message...\nwhich contains multiple lines to be displayed and automatically word wrapped.");
        }

        void OnItemTap(object sender, EventArgs e)
        {
            TextItem item = sender as TextItem;
            Dialog dlg = new TextDialog(item.Text);
            dlg.Hidden += (s, ee) => dlg.Dispose();
            dlg.ShowModal();
        }

        protected override void OnHidden()
        {
            base.OnHidden();
        }

        protected override void OnOrientationChanged(object sender, EventArgs e)
        {
            base.OnOrientationChanged(sender, e);
            myToolbar.ItemsPerRow = Orientation == Orientation.Landscape ? 2 : 3;
        }

    }
}
